package com.example.demo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/Param")
@RestController
public class ParamController {
    @RequestMapping("/m1")
    public String m1(String name) {
        return "接受到的参数name:" + name;
    }

    @RequestMapping("/m2")
    public String m2(String name, Integer age) {
        return "接受到的参数name:" + name + ",age:" + age;
    }

    @RequestMapping("/m3")
    public String m3(String name, int age) {
        return "接受到的参数name:" + name + ",age:" + age;
    }

    @RequestMapping("/m4")
    public String m4(Person person) {
        return "接收到的参数person:" + person.toString();
    }

    @RequestMapping("/m5")
    //@RequestParam注解设置为必传参数,required = false设置为非必传参数
    public String m5(@RequestParam(value = "username", required = false) String name) { //类似于重命名
        return "接收到的参数name:" + name;
    }

    @RequestMapping("/m6")
    public String m6(String[] arrayParam) {
        return "接收到的参数arrayParam:" + Arrays.toString(arrayParam) + ",长度:" + arrayParam.length;
    }

    @RequestMapping("/m7")
    public String m7(@RequestParam(required = false) List<String> listParam) {
        //此处默认封装成数组,需要使用@RequeatParam注解
        return "接收到的参数listParam:" + listParam + ",长度:" + listParam.size();
    }

    @RequestMapping("/m8")
    public String m8(@RequestBody Person person) { //@RequestBody:请求正文,传递json数据时使用
        return "接收的数据person:" + person.toString();
    }

    @RequestMapping("/m9/{userId}/{name}")
    public String m9(@PathVariable Integer userId, @PathVariable("name") String username) { //@PathVariable获取URL中的参数
        return "userId:" + userId + ",username:" + username;
    }

    @RequestMapping("/m10")
    public String m10(@RequestPart MultipartFile file) throws IOException { //上传文件
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:/alyun/" + file.getOriginalFilename()));
        return "success";
    }

    //servlet原生方式获取所有cookie
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) { //Cookie从request里来
        Cookie[] cookies = request.getCookies();
//        for (Cookie cookie : cookies) {
//            System.out.println(cookie.getName()+":"+cookie.getValue());
//        }
        if (cookies != null) { //lambda表达式
            Arrays.stream(cookies).forEach(cookie -> {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            });
        }

        return "获取cookie成功";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue String bite, @CookieValue String aaa) {
        return "cookie存取的值bite:" + bite + ",aaa:" + aaa;
    }

    //session是服务器端保存用户信息的一种机制,因此需要手动创建,无法进行伪造
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("username", "lizhixiong");
        return "success";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        //HttpSession session = request.getSession(true);//参数设置为true,即便session不存在也会创建session
        //HttpSession session = request.getSession(false);//参数设置为false,session不存在即不会创建session
        HttpSession session = request.getSession(false); //默认值为true
        if (session != null) {
            String username = (String) session.getAttribute("username");
            return "登录用户:" + username;
        }
        return "session 为空";
    }

    @RequestMapping("/getSession2")
    //@SessionAttribute默认是必传参数,如果传的参数未赋值则会报错
    public String getSession2(@SessionAttribute(required = false) String username) {
        return "username:" + username;
    }

    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session) {
        //等同于HttpSession session = request.getSession(true);HttpSession属于内置对象
        String username = (String) session.getAttribute("username");
        return "登录用户:" + username;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:"+userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "userAgent:"+userAgent;
    }
}
